/**
 * 
 */
package org.gov.vis.master.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import org.gov.vis.components.VISHyperLink;
import org.gov.vis.gui.VISDataGrid;
import org.gov.vis.main.Main;
import org.gov.vis.master.controller.DepartmentCtlr;
import org.gov.vis.master.controller.DepartmentCtlrImpl;
import org.gov.vis.master.dto.DeptDto;
import org.gov.vis.master.gui.EditDepartmentDialog;
import org.gov.vis.master.gui.ViewDepartmentDialog;
import org.gov.vis.util.Util;

/**
 * @author user
 *
 */
public class ViewDepartmentListener implements ActionListener,BaseListener {


	
	private DepartmentCtlr departmentCtlr = new DepartmentCtlrImpl();
	private ViewDepartmentDialog viewDepartmentDialog;
	private DeptDto departmentDto;
	final private ResourceBundle rb = Util.getResources();
	private EditDepartmentDialog editDepartmentDialog;

	public ViewDepartmentListener(ViewDepartmentDialog viewDepartmentDialog) {
		// TODO Auto-generated constructor stub
		this.viewDepartmentDialog = viewDepartmentDialog;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setGuiDetailsToDto(Object _guiObject) {
		// TODO Auto-generated method stub
		ViewDepartmentDialog _viewDepartmentDialog =(ViewDepartmentDialog)_guiObject;
		departmentDto = new DeptDto();
		
		String code = _viewDepartmentDialog.getCodeTxtBx().getText();
		if(code!=null){
		 departmentDto.setCode(code);
		}
		/*otherDto.setNameOfOrg(_othersDialog.getTxtFldOrg().getText());
		otherDto.setNameOfAIWorker(_othersDialog.getTxtFldAIWorker().getText());
		otherDto.setHeadQuarter(_othersDialog.getTxtFldHeadQuarter().getText());
		otherDto.setJuridiction(_othersDialog.getTxtFldJurisdiction().getText());
		otherDto.setMandal(_othersDialog.getTxtFldMandal().getText());
		otherDto.setDistrict(_othersDialog.getTxtFldDistrict().getText());*/
	}

	@Override
	public void setDtoDetailsToGui(Object _dtoObject) {
		// TODO Auto-generated method stub
		DeptDto _departmentDto = (DeptDto)_dtoObject;
		if(editDepartmentDialog != null){
			editDepartmentDialog.getTxtFldCode().setText(_departmentDto.getCode());
			editDepartmentDialog.getTxtFldDistrict().setText(_departmentDto.getDistrict());
			editDepartmentDialog.getTxtFldVillage().setText(_departmentDto.getVillage());
			editDepartmentDialog.getTxtFldManadal().setText(_departmentDto.getMandal());
			editDepartmentDialog.getTxtFldNameOfInst().setText(_departmentDto.getInstitution());
			editDepartmentDialog.setHidnObject(_departmentDto.getDeptId());
		}
	}

	@Override
	public void clearGUILabelColors(Color _labelColor) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		// TODO Auto-generated method stub
		
		
		if(actionEvent.getActionCommand().equals("Search")){
			setGuiDetailsToDto(viewDepartmentDialog);
			try {
				
			List<DeptDto> departmentLst = departmentCtlr.searchDepartments(departmentDto);
			
			List<String> colLst = new ArrayList<String>();
			colLst.add("Code");
			colLst.add("Name of Institution ");
			colLst.add("Village");
			colLst.add("Mandal");
			colLst.add("District");
			
			ArrayList<Object> dataList = new ArrayList<Object>(1);
			if(departmentLst != null && !departmentLst.isEmpty()){
				for(DeptDto dto:departmentLst){
					ArrayList<Object> lst = new ArrayList<Object>(1);
					//setting each property of the current Accessory object (row) to the list
					lst.add(dto);
					lst.add(dto.getCode());
					lst.add(dto.getInstitution());
					lst.add(dto.getVillage());
					lst.add(dto.getMandal());
					lst.add(dto.getDistrict());
					dataList.add(lst);
				}//end for 
			}//end if
			VISDataGrid.createDataPanel(dataList, colLst, viewDepartmentDialog.getPnlDepartmentResults(),this);
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		if(actionEvent.getSource() instanceof VISHyperLink){
			actionEvent.getSource();
			VISHyperLink visHyperLink = (VISHyperLink)actionEvent.getSource();
			DeptDto departmentDto = (DeptDto)visHyperLink.getObject();
			//System.out.println("action from the object----"+departmentDto.getCode());
			editDepartmentDialog = new EditDepartmentDialog(Main.getMainFrame(),
                    rb.getString("dialog_edit_department_title"),
                    false);
			setDtoDetailsToGui(departmentDto);
			viewDepartmentDialog.dispose();
		}
		
	}





}
